<template>

  <el-card>
    <template #header>
      <span>{{ qrcode.title }}</span>
    </template>

    <div class="qrcode-wrapper">
      <div @click="qrcodeClickHandle">
        <qrcode-vue :value="qrcode.contents" :size="size" level="H"/>
      </div>
      <div class="qrcode-mask" v-show="qrcode.showMask" @click="qrcodeMaskClickHandle"></div>
    </div>

    <template #footer>
      <el-button-group class="ml-4">
        <el-button @click="editClickHandle">Edit</el-button>
        <el-button @click="deleteClickHandle">Delete</el-button>
      </el-button-group>
    </template>
  </el-card>

</template>

<script>
import QrcodeVue from 'qrcode.vue'
import {ElMessageBox} from "element-plus";

export default {
  props: {
    qrcode: {
      type: Object,
      default: () => {}
    }
  },
  components: {
    QrcodeVue,
  },
  data() {
    return {
      size: 300,
      date: '',
      time: ''
    }
  },
  created() {
    const d = new Date(this.timestamp);
    this.date = `${d.toLocaleDateString()}`;
    this.time = d.toLocaleTimeString();
  },
  methods: {
    editClickHandle() {
      this.$emit('editItem', {qrcode: this.qrcode});
    },
    deleteClickHandle() {
      ElMessageBox.confirm(
          'Confirm delete?',
          '警告', {
            confirmButtonText: 'Ok',
            cancelButtonText: 'Cancel',
            type: "warning"
          }
      ).then((result) => {
        if (result.value) {
          this.$emit('deleteItem', this.qrcode)
        }
      })
    },
    qrcodeClickHandle() {
      this.qrcode.showMask = !this.qrcode.showMask
      this.$emit('showQrcodeMask', this.qrcode);
    },
    qrcodeMaskClickHandle() {
      this.qrcode.showMask = !this.qrcode.showMask
      this.$emit('hideQrcodeMask', this.qrcode);
    },
  }
}
</script>

<style scoped>

.qrcode-wrapper {
  position: relative;
  cursor: pointer;
}

.qrcode-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #101010;
  opacity: 0.99;
}

</style>